package tuiLayer;

import java.util.*;
import controlLayer.*;
import modelLayer.*;
import modelLayer.Exceptions.*;

public class AccountUI {
    private AccountCtrl accountCtrl;
    
    
    public AccountUI() {
        accountCtrl=new AccountCtrl();
    }
    
    public void start() {
        mainMenuAccount();
    }
    
    private void mainMenuAccount()  {
        boolean stop=false;
        while(!stop) {
            int choice = writeMainMenuAccount();
            switch (choice) {
                case 0:
                    stop=true;
                    break;
                case 1:
                    createAccount();
                    break;
                case 2:
                    transferFunds();
                    break;
                case 3:
                    showAccountInfo();
                    break;
                default: System.out.println("enter a valid choice");
            }
        }
    }
    
    private int writeMainMenuAccount() {
        Scanner keyboard= new Scanner(System.in);
        System.out.println("\n*****Account menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create Account");
        System.out.println("(2) Transfer Funds");
        System.out.println("(3) Show info about an account");
        System.out.println("\n Type your choice");
        int choice= keyboard.nextInt();
        return choice;
    }
    
    private void createAccount() {
        Scanner keyboard= new Scanner (System.in);
        System.out.println("Type account name");
        String name= keyboard.nextLine();
        
        System.out.println("Type the balance of the account");
        double balance = keyboard.nextDouble();
        try {
            accountCtrl.createAccount(name, balance);
        }
        catch (DuplicateAccountNameException e) {
            System.out.println(e.getMessage());
            createAccount();
        }
        System.out.println("\nAccount successfully created!");
    }
    
    private void transferFunds(){
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println("Type the account name FROM where you want to transfer");
        String nameFrom=keyboard.nextLine();
        
        System.out.println("Type the account name TO where you want to transfer");
        String nameTo=keyboard.nextLine();
        
        System.out.println("Type the amount you want to tranfer");
        double amount=keyboard.nextDouble();
        
        try{
            accountCtrl.transferFunds(nameFrom, nameTo, amount);
        }
        catch (BalanceUnderrunException e) {
            System.out.println(e.getMessage());
            transferFunds();
        }
        
    }
    private void showAccountInfo() {
        Scanner keyboard= new Scanner(System.in);
        System.out.println("Type the account name");
        String name=keyboard.nextLine();
        accountCtrl.showInfo(name);
    }
}
